package com.xbjtech.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.xbjtech.mapper.HxmMapper;
import com.xbjtech.mapper.OrderMapper;
import com.xbjtech.mapper.StoreMapper;
import com.xbjtech.model.Hxm;
import com.xbjtech.model.Order;
import com.xbjtech.model.OrderExample;
import com.xbjtech.model.Store;
import com.xbjtech.service.HxmRandomService;
import com.xbjtech.service.OrderService;
import com.xbjtech.service.SmsService;

@Service
public class OrderServiceImpl implements OrderService {

	@Autowired
	OrderMapper orderMapper;
	
	@Autowired
	HxmRandomService hxmRandomService;
	
	@Autowired
	HxmMapper hxmMapper;
	
	@Autowired
	SmsService smsService;
	
	@Autowired
	StoreMapper storeMapper;
	
	@Override
	public List<Order> getOrdersPaged(OrderExample example, int page, int rows) {
		PageHelper.startPage(page, rows);
		return orderMapper.selectByExample(example);
	}

	@Override
	public void insertOrder(Order order) throws Throwable {
		orderMapper.insert(order);
	}

	@Override
	public Order getOrderById(Long id) {
		return orderMapper.selectByPrimaryKey(id);
	}

	// update t_order set sdf=dsaf,dsfaf=asdfa where id =#{value}
	@Override
	public void updateOrder(Order order) throws Exception {
		orderMapper.updateByPrimaryKeySelective(order);
	}

	@Override
	public void deleteOrderById(Long id) throws Exception {
		orderMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<Order> getAllOrder(OrderExample example) {
		// 查前1W条记录
		// 确保数量太大的时候，服务器不会宕机
		PageHelper.startPage(1, 10000);
		return orderMapper.selectByExample(example);
	}

	@Override
	public void saveOrders(List<Order> orders) throws Exception {
		for (Order order : orders) {
			orderMapper.insert(order);
		}
	}

	@Override
	public void pd(Order order) throws Exception {
		String random = hxmRandomService.random();
		Hxm hxm = hxmMapper.getHxmByHxmstring(random);
		hxm.setOrderId(order.getId());
		hxmMapper.updateHxmByPrimaryKey(hxm);
		order.setHxm(hxm.getHxm()); // 记录订单的核销码
		orderMapper.updateByPrimaryKeySelective(order);
		Store store = storeMapper.selectByPrimaryKey(order.getStoreId());
		// 异步发送短信，不让发送短信产生的网络等问题影响主程序的等待
		// 启动一个线程来负责发送短信
		new Thread(new Runnable() {
			@Override
			public void run() {
				// 给车主发短信
				Map<String, String> params = new HashMap<>();
				params.put("code", hxm.getHxm());
				params.put("name", order.getStoreName());
				params.put("phone", store.getPhone());
				params.put("address", store.getAddress());
				smsService.sendMsg("475460", order.getPhone(), params);
				
				// 给门店发短信
				params.clear();
				params.put("orderId", order.getOrderId());
				StringBuffer sb = new StringBuffer();
				sb.append("车主：").append(order.getName())
				.append("，手机：").append(order.getPhone())
				.append("，车型：").append(order.getCarType())
				.append(", 安装产品：").append(order.getProduct())
				.append("， 备注：").append(order.getRemark());
				params.put("orderInfo", sb.toString());
				smsService.sendMsg("475461", store.getPhone(), params);
			}
		}).start();
	}

	@Override
	public void hx(Order order, Hxm hxm) throws Exception {
		orderMapper.updateByPrimaryKeySelective(order);
		hxmMapper.deleteHxmByPrimaryKey(hxm.getId());
	}

}
